﻿jQuery(document).ready(function () {
    step1 = jQuery('#Step1');
    step2 = jQuery('#Step2');
    step3 = jQuery('#Step3');

    var currentTime = new Date();
    var minimumDate = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate());

    AttachDateRange('txtBeginDate', 'txtEndDate');

    numberSteps = jQuery('div[id^=\'Step\']').length;

    initWizard();
    initValidation();

    document.onkeypress = stopRKey;
});

function initWizard() {
    jQuery('#btnPrevious').live('click', function () {
        currentStep = currentStep - 1;
        navWizard();
    });

    jQuery('#btnNext').live('click', function () {
        currentStep = currentStep + 1;
        navWizard();
    });

    jQuery('#btnCancel').live('click', function () {
        return confirm(cancelConfirmMsg);
    });

    jQuery('#wizard div[id^=\'Step\']').each(function (i) { jQuery(this).hide(); });

    jQuery('#Step' + currentStep).show();

    initWizardButtons();

    initCodeCampForm();

    initTrackForm();

    initRoomForm();

    jQuery('.titleHintBox').inputHintBox({ div: jQuery('#shiny_box'), div_sub: '.shiny_box_body', source: 'attr', attr: 'title', incrementLeft: 5 });

    jQuery('.cc_delete').live('click', function () { jQuery(this).parent().remove(); });
}

function navWizard() {
    switch (currentStep) {
        case 2:
            if (!validateCodeCamp()) return false;
            break;
        case 3:
            if (!validateTracks()) return false;
            break;
        case 4:
            if (!validateRooms()) return false;
            break;
        default:
            break;
    }

    jQuery('div[id^=\'Step\']').hide();
    jQuery('#Step' + currentStep).show();
    initWizardButtons();
}

function initWizardButtons() {
    jQuery('a.cc-button').show();

    if (currentStep == 1) jQuery('#btnPrevious').hide();
    if (currentStep == numberSteps) jQuery('#btnNext').hide();
}

function AttachDateRange(startDate, endDate) {
    var currentTime = new Date();
    var _minDate = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate());
    var _maxDate = new Date(currentTime.getFullYear() + 1, currentTime.getMonth(), currentTime.getDate());

    jQuery("#" + startDate + ",#" + endDate).datepicker({
        beforeShow: function customRange(input) {
            return { minDate: (input.id == endDate ? (jQuery("#" + startDate).datepicker("getDate") != null ? jQuery("#" + startDate).datepicker("getDate") : _minDate) : _minDate),
                maxDate: (input.id == startDate ? (jQuery("#" + endDate).datepicker("getDate") != null ? jQuery("#" + endDate).datepicker("getDate") : _maxDate) : _maxDate)
            };
        },
        defaultDate: +0,
        hideIfNoPrevNext: true,
        showOn: "both",
        firstDay: 1,
        changeFirstDay: false
    });
}

function initCodeCampForm() {
    jQuery('#txtMaximumCapacity').mask('?99999', { placeholder: ' ' });

    var acOptions = { serviceUrl: rolesHandler, minChars: 2 };
    jQuery('#txtRoleName').autocomplete(acOptions);
    jQuery('#txtWaitListRoleName').autocomplete(acOptions);
}

function initTrackForm() {
    jQuery('#lnkAddTrack').live('click', function () {
        jQuery('#pAddTrack').clone(true).attr('id', 'pAddTrack' + trackCount).insertBefore('#lnkAddTrack').end().show().children('input:text').focus();
        jQuery('#pAddTrack' + trackCount + ' input').each(function (i) {
            jQuery(this).attr('id', jQuery(this).attr('id') + trackCount);
        });
        trackCount = trackCount + 1;
    });
}

function initRoomForm() {
    jQuery('#lnkAddRoom').live('click', function () {
        jQuery('<hr />').css('margin', '15px').insertBefore('#lnkAddRoom');
        jQuery('#pRoomName').clone(true).insertBefore('#lnkAddRoom').attr('id', 'pRoomName' + roomCount);
        jQuery('#pRoomName' + roomCount + ' input').each(function (i) {
            jQuery(this).attr('id', jQuery(this).attr('id') + roomCount);
        });
        jQuery('#pRoomDescription').clone(true).insertBefore('#lnkAddRoom').attr('id', 'pRoomDescription' + roomCount);
        jQuery('#pRoomDescription' + roomCount + ' input').each(function (i) {
            jQuery(this).attr('id', jQuery(this).attr('id') + roomCount);
        });
        jQuery('#pRoomName' + roomCount + ',#pRoomDescription' + roomCount).children(':input').die();
        jQuery('#pRoomName' + roomCount + ',#pRoomDescription' + roomCount).children(':input').unbind();
        jQuery('#pRoomName' + roomCount).children(':input:first').focus();
        roomCount = roomCount + 1;
    });
}

function initValidation() { jQuery('[id$=errmsg]').hide(); }

function validateCodeCamp() {
    var errCount = 0;
    errCount = errCount + requireField('txtTitle');
    errCount = errCount + requireField('txtDescription');
    errCount = errCount + requireField('txtBeginDate');
    errCount = errCount + requireField('txtEndDate');
    errCount = errCount + requireField('txtRoleName');
    errCount = errCount + requireField('txtWaitListRoleName');
    errCount = errCount + requireField('txtMaximumCapacity');
    if (errCount > 0) {
        return false;
    } else {
        return true;
    }
}

function validateTracks() {
    var errCount = 0;

    errCount = errCount + requireField('txtTrack');

    for (i = 0; i <= trackCount - 1; i++) {
        errCount = errCount + requireField('txtTrack' + i);
    }

    if (errCount > 0) {
        return false;
    } else {
        return true;
    }
}

function validateRooms() {
    var errCount = 0;

    errCount = errCount + requireField('txtRoomName');
    errCount = errCount + requireField('txtRoomCapacity');
    errCount = errCount + requireField('txtRoomDescription');

    for (i = 0; i <= roomCount - 1; i++) {
        errCount = errCount + requireField('txtRoomName' + i);
        errCount = errCount + requireField('txtRoomCapacity' + i);
        errCount = errCount + requireField('txtRoomDescription' + i);
    }

    if (errCount > 0) {
        return false;
    } else {
        return true;
    }
}

function requireField(obj) {
    var oField = jQuery('#' + obj);
    var oId = oField.attr('id');
    if (oField.val() == '') {
        var re = /\d+$/;
        if (re.test(oId)) {
            jQuery('#' + oId.replace(re.exec(oId), '') + '-errmsg' + re.exec(oId)).show();
        } else {
            jQuery('#' + oId + '-errmsg').show();
        }
        return 1;
    }
    return 0;
}

function stopRKey(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13) && (node.type == 'text')) { return false; }
}